package com.wugui.datax.rpc.remoting.invoker.call;

/**
 * @author xuxueli 2018-10-23
 */
public abstract class XxlRpcInvokeCallback<T> {

  private static final ThreadLocal<XxlRpcInvokeCallback<?>> THREAD_INVOKER_FUTURE =
      new ThreadLocal<>();

  /**
   * get callback
   *
   * @return
   */
  public static XxlRpcInvokeCallback<?> getCallback() {
    XxlRpcInvokeCallback<?> invokeCallback = THREAD_INVOKER_FUTURE.get();
    THREAD_INVOKER_FUTURE.remove();
    return invokeCallback;
  }

  // ---------------------- thread invoke callback ----------------------

  /**
   * set future
   *
   * @param invokeCallback
   */
  public static void setCallback(XxlRpcInvokeCallback<?> invokeCallback) {
    THREAD_INVOKER_FUTURE.set(invokeCallback);
  }

  /** remove future */
  public static void removeCallback() {
    THREAD_INVOKER_FUTURE.remove();
  }

  public abstract void onSuccess(T result);

  public abstract void onFailure(Throwable exception);
}
